<template>
  <!-- 日报查询 DLR -->
  <div>
    <TopFilter :list="topFilterList" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhDealerquiry"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
        :isSelectColumn="false"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="visible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SonModel :type="handleType" :rowData="formData" @close="emitCloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils';
import { queryList, exportExcel ,exporturl} from '@/api/dealerSale/DealerDaily/Dealerquiry';
import SonModel from './SonModel';

export default {
  name: 'Dealerquiry',
  mixins: [dictionary],
  components: {
    SonModel
  },

  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      exportLoading: false,
      visible: false,
      handleType: '',
      drawerTitle: ''
    };
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },

  methods: {
    emitCloseHandler(val) {
      this.visible = val;
    },
    detailHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    // 导出
    async exportHandle() {

       let json = Object.assign({}, { CODE: 'CODE_0000055' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '日报统计车型');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    createdFrom() {
      return [
        {
          type: 'RANGE_DATE', // 别忘记处理查询一天查询不到数据问题
          label: '上报日期',
          fieldName: 'applyDateStart|applyDateEnd',
          dateType: 'exactdaterange',
          placeholder: '上报日期'
        },
        {
          type: 'SELECT',
          label: '上报状态',
          fieldName: 'dailyStatus',
          placeholder: '请选择',
          itemList: this.createDictList('6062')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '经销商简称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '区域经理',
          dataIndex: 'regionalManager',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上报状态',
          dataIndex: 'dailyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6062'),
          render: props => {
            return <span>{this.createDictText(props.row.dailyStatus, '6062')}</span>;
          }
        },
        {
          title: '上报日期',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'YYYY-MM-DD'
        },
        {
          title: '上报人员',
          dataIndex: 'checkPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '区域审批时间',
          dataIndex: 'regionalCheckDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大区或办事处审批时间',
          dataIndex: 'officeCheckDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
